package com.eling.elcms.travel.service.impl;

import com.eling.elcms.basedoc.model.BusinessMessage;
import com.eling.elcms.basedoc.model.Room;
import com.eling.elcms.basedoc.model.TargetType;
import com.eling.elcms.basedoc.model.TaskType;
import com.eling.elcms.basedoc.service.IBusinessMessageManager;
import com.eling.elcms.basedoc.service.IRoomManager;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.travel.dao.ICheckOutDealDao;
import com.eling.elcms.travel.model.*;
import com.eling.elcms.travel.model.CheckInImplement.PaymentMethod;
import com.eling.elcms.travel.model.CheckInImplement.StatementStatus;
import com.eling.elcms.travel.model.CheckOutDeal.Status;
import com.eling.elcms.travel.model.view.ChangeRoomSettlementView;
import com.eling.elcms.travel.model.view.CheckOutDealSaveView;
import com.eling.elcms.travel.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class CheckOutDealManagerImpl extends GenericManagerImpl<CheckOutDeal, Long> implements ICheckOutDealManager {

	ICheckOutDealDao checkOutDealDao;

	@Autowired
	private IRoomManager roomManager;

	@Autowired
	private ICheckInImplementManager checkInImplManager;

	@Autowired
	private ICheckOutSettlementManager checkOutSetlManager;

	@Autowired
	private ITravelContractManager travelContractManager;

	@Autowired
	private IRoomOccupancyManager roomOccupancyManager;
	
	@Autowired
 	private IBusinessMessageManager businessMessageManager;

	@Autowired
	public void setCheckOutDealDao(ICheckOutDealDao checkOutDealDao) {
		this.dao = checkOutDealDao;
		this.checkOutDealDao = checkOutDealDao;
	}

	@Override
	public CheckOutDeal saveAll(CheckOutDealSaveView view) {
		List<CheckOutSettlement> checkOutSetlList = view.getSettlementList();
		checkOutSetlList = checkOutSetlManager.save(checkOutSetlList);
		Boolean flag = true;
		for (CheckOutSettlement cos : checkOutSetlList) {
			if (!cos.getCheckOutSettlement()) {
				flag = false;
			}
		}

		CheckOutDeal checkOutDeal = view.getCheckOutDeal();
		CheckInImplement getcImplement = view.getcImplement();

		// 子表全部结算完成修改主表状态
		if (flag) {
			checkOutDeal.setStatementStatus(StatementStatus.Statement);
			checkOutDeal.setStatus(Status.NoConfirm);
			checkOutDeal = save(checkOutDeal);

			// 退房结算时把房间状态改为脏房
			CheckInImplement checkInImplement = checkInImplManager
					.get(checkOutDeal.getCheckInImplement().getPkCIImplement());

			checkInImplement.setStatementStatus(CheckInImplement.StatementStatus.Statement);
			checkInImplement.setStatementDate(new Date());
			if (getcImplement != null) {
				checkInImplement.setCompany(getcImplement.getCompany());
				checkInImplement.setPerName(getcImplement.getPerName());
				checkInImplement.setPhone(getcImplement.getPhone());
				checkInImplement.setOnCreditMoney(getcImplement.getOnCreditMoney());
			}
			checkInImplManager.save(checkInImplement);

			Set<RoomPersonalInfo> rmPerInfoSet = checkInImplement.getRoomPersonalInfos();
			List<Room> rmList = new ArrayList<>();
			for (RoomPersonalInfo rmPerInfo : rmPerInfoSet) {
				if (!rmPerInfo.getCheckIn()) {
					continue;
				}
				Room rm = rmPerInfo.getRoomRegister().getRoom();
				rm.setStatus(Room.Status.Dirty);
				rmList.add(rm);
				// 如果用了权证 扣除余额
				if (rmPerInfo.getNonMemberRegister().getSignUpRegister().getTravelContract() != null) {
					TravelContract travelContract = rmPerInfo.getNonMemberRegister().getSignUpRegister()
							.getTravelContract();
					CheckOutSettlement checkOutSettlement = rmPerInfo.getCheckOutSettlement();
					if (checkOutSettlement.getBedSubsidyUnit() != null
							&& checkOutSettlement.getBedSubsidyUnit() != 0D) {
						travelContract.setBalTime(travelContract.getBalTime() - checkOutSettlement.getBedSubsidyUnit());
					} else if (checkOutSettlement.getBedSubsidy() != null && checkOutSettlement.getBedSubsidy() != 0D) {
						travelContract.setBalance(travelContract.getBalance() - checkOutSettlement.getExpense());
					}
					travelContractManager.save(travelContract);

				}

			}
			businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.CheckOutDealList,checkOutDeal.getPkCheckOutDeal()));

			// 还原房态为脏房
			if (!rmList.isEmpty()) {
				roomManager.save(rmList);
			}
			// 删除房间占用数据
			roomOccupancyManager.removeRoomOccup(checkInImplement.getNonMemGroupReserve().getNonMemberGroup());
		} else {
			checkOutDeal.setStatementStatus(StatementStatus.NoStatement);
			checkOutDeal = save(checkOutDeal);
		}

		return checkOutDeal;
	}

	@Override
	public List<ChangeRoomSettlementView> queryChangeRoom(Long pkCIImplement) {
		return checkOutDealDao.queryChangeRoom(pkCIImplement);
	}

	@Override
	public CheckOutDeal queryBypkCiimplement(Long pkCIImplement) {
		CheckInImplement checkInImplement = checkInImplManager.get(pkCIImplement);
		CheckOutDeal checkOutDeal = new CheckOutDeal();
		checkOutDeal.setCheckInImplement(checkInImplement);
		 List<CheckOutDeal> checkOutDeals = checkOutDealDao.query(checkOutDeal);
		 System.out.println(checkOutDeals.size());
		 if(!checkOutDeals.isEmpty()){
			 checkOutDeal = checkOutDeals.get(0);
		 }
		 return checkOutDeal;
	}

	@Override
	public List<CheckOutDeal> queryByPaymentMethod(CheckOutDeal cond,String paymentMethod) {
		List<CheckOutDeal> returnList = new ArrayList<>();
		List<CheckOutDeal> checkOutDeals = query(cond);
		if("all".equals(paymentMethod)){
			return checkOutDeals; 
		}
		for(CheckOutDeal c : checkOutDeals){
			for(PaymentMethod p: c.getPaymentMethods()){
				if(paymentMethod.equals(p.toString())){
					returnList.add(c);
					break;
				}
			}
		}
		return returnList;
	}

}
